package br.com.players.resource;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import br.com.players.model.Time;
import br.com.players.service.CadastroTimeService;

@Path("/cadastro/time")
public class CadastroTime {
	
	@POST
	@Path("/inserir")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public String cadastrarTime(@FormParam("nome") String nome, @FormParam("capitao") String capitao) {
		Time time = new Time();
		
		time.setNome(nome);
		time.setCapitao(capitao);
		
		Time timeCadastrado = new CadastroTimeService().buscarTimePeloNome(nome);
		
		if(timeCadastrado == null) {
			new CadastroTimeService().inserirTime(time);
			return "sucesso";
		} else {
			return "falha";
		}
	}
	
	@GET
	@Path("/buscar/todos")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Time> buscarTimes() {
		return new CadastroTimeService().listarTimes();
	}
	
	@GET
	@Path("/buscar/todos/capitao")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Time> buscarTimesByCapitao(@QueryParam("capitao") String capitao) {
		return new CadastroTimeService().listarTimesByCapitao(capitao);
	}
	
	@GET
	@Path("/buscar")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public Time buscarTime(@QueryParam("nome") String nome) {
		return new CadastroTimeService().buscarTimePeloNome(nome);
	}
	
	@POST
	@Path("/timePorCapitao")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public List<Time> timePorCapitao(@FormParam("capitao") String capitao) {
		 return new CadastroTimeService().timePorCapitao(capitao);
	}
	
	@POST
	@Path("/remover")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public void removerTime(@FormParam("id") String id) {
		Time time = new Time();
		time.setId(Integer.parseInt(id));
		
		new CadastroTimeService().removerTime(time);
	}
	
	@POST
	@Path("/atualizar/nome")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public void atualizarTime(@FormParam("id") String id, @FormParam("nome") String nome) {
		Time time = new Time();
		time.setId(Integer.parseInt(id));
		time.setNome(nome);
		
		new CadastroTimeService().atualizarTime(time);
	}
}
